#include "stdafx.h"
#include "BaseModel.h"

namespace wlog {

	using namespace System;
	using namespace System::Collections;
	using namespace System::Reflection;

	Hashtable^ BaseModel::InitFields(void)
	{
		String^ today = System::DateTime::Now.ToString();

		Hashtable^ fs = this->GetFields();
		Type^ t = this->GetType();

		try
		{
			FieldInfo^ f_date_add = t->GetField("date_add");
			if (f_date_add != nullptr && f_date_add->GetValue(this)->ToString()->Equals(""))
			{
				fs["date_add"] = today;
			}

			FieldInfo^ f_date_upd = t->GetField("date_upd");
			if (f_date_upd != nullptr)
			{
				fs["date_upd"] = today;
			}
		}
		catch (NullReferenceException^ e)
		{
		}

		try
		{
			FieldInfo^ f_active = t->GetField("active");
			if (f_active != nullptr)
			{
				fs["active"] = f_active->GetValue(this)->ToString();
			}
		}
		catch (NullReferenceException^ e)
		{
		}

		return fs;
	}

	String^ BaseModel::SafeValue(String^ value)
	{
		return TL_Libs::MySql::safeValue(value);
	}
}